package com.cars.hdfs.service;

import com.alibaba.fastjson2.JSON;
import jakarta.annotation.Resource;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: HdfsService
 * @Description: HDFS接口
 * @author: caoqianfan
 * @date: 2023/9/6 16:02
 */
@Service
public class HdfsService {

    @Resource
    private FileSystem fs;
    private static final int bufferSize = 1024 * 1024 * 64;

    /**
     * 在HDFS创建文件夹
     *
     * @param path 文件路径
     * @return boolean
     * @throws Exception 异常
     */
    public boolean mkdir(String path) throws Exception {
        if (path.isBlank()) {
            return false;
        }
        if (existFile(path)) {
            return true;
        }

        // 目标路径
        Path srcPath = new Path(path);
        boolean isOk = fs.mkdirs(srcPath);
        fs.close();
        return isOk;
    }

    /**
     * 判断HDFS文件是否存在
     *
     * @param path 文件路径
     * @return boolean
     * @throws Exception 异常
     */
    public boolean existFile(String path) throws Exception {
        if (path.isBlank()) {
            return false;
        }

        Path srcPath = new Path(path);
        return fs.exists(srcPath);
    }

    /**
     * 读取HDFS目录信息
     *
     * @param path 文件路径
     * @return {@code List<Map<String, Object>>}
     * @throws Exception 异常
     */
    public List<Map<String, Object>> readPathInfo(String path) throws Exception {
        if (path.isBlank()) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }

        // 目标路径
        Path newPath = new Path(path);
        FileStatus[] statusList = fs.listStatus(newPath);
        List<Map<String, Object>> list = new ArrayList<>();
        if (null != statusList && statusList.length > 0) {
            for (FileStatus fileStatus : statusList) {
                Map<String, Object> map = new HashMap<>();
                map.put("filePath", fileStatus.getPath());
                map.put("fileStatus", fileStatus.toString());
                list.add(map);
            }
            return list;
        } else {
            return null;
        }
    }

    /**
     * HDFS创建文件
     *
     * @param path 文件路径
     * @param file 文件
     * @throws Exception 异常
     */
    public void createFile(String path, MultipartFile file) throws Exception {
        if (path.isBlank() || file.getBytes().length == 0) {
            return;
        }
        String fileName = file.getOriginalFilename();

        // 上传时默认当前目录，后面自动拼接文件的目录
        Path newPath = new Path(path + "/" + fileName);
        // 打开一个输出流
        FSDataOutputStream outputStream = fs.create(newPath);
        outputStream.write(file.getBytes());
        outputStream.close();
        fs.close();
    }

    /**
     * 读取HDFS文件内容
     *
     * @param path 文件路径
     * @return {@code String}
     * @throws Exception 异常
     */
    public String readFile(String path) throws Exception {
        if (path.isBlank()) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }

        // 目标路径
        Path srcPath = new Path(path);
        try (FSDataInputStream inputStream = fs.open(srcPath)) {
            // 防止中文乱码
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            String lineTxt;
            StringBuilder sb = new StringBuilder();
            while ((lineTxt = reader.readLine()) != null) {
                sb.append(lineTxt);
            }
            return sb.toString();
        } finally {
            fs.close();
        }
    }

    /**
     * 读取HDFS文件列表
     *
     * @param path 文件路径
     * @return {@code List<Map<String, String>>}
     * @throws Exception 异常
     */
    public List<Map<String, String>> listFile(String path) throws Exception {
        if (path.isBlank()) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }


        // 目标路径
        Path srcPath = new Path(path);
        // 递归找到所有文件
        RemoteIterator<LocatedFileStatus> filesList = fs.listFiles(srcPath, true);
        List<Map<String, String>> returnList = new ArrayList<>();
        while (filesList.hasNext()) {
            LocatedFileStatus next = filesList.next();
            String fileName = next.getPath().getName();
            Path filePath = next.getPath();
            Map<String, String> map = new HashMap<>();
            map.put("fileName", fileName);
            map.put("filePath", filePath.toString());
            returnList.add(map);
        }
        fs.close();
        return returnList;
    }

    /**
     * HDFS重命名文件
     *
     * @param oldName 文件老名字
     * @param newName 文件新名字
     * @return boolean
     * @throws Exception 异常
     */
    public boolean renameFile(String oldName, String newName) throws Exception {
        if (oldName.isBlank() || newName.isBlank()) {
            return false;
        }

        // 原文件目标路径
        Path oldPath = new Path(oldName);
        // 重命名目标路径
        Path newPath = new Path(newName);
        boolean isOk = fs.rename(oldPath, newPath);
        fs.close();
        return isOk;
    }

    /**
     * 删除HDFS文件
     *
     * @param path 文件路径
     * @return boolean
     * @throws Exception 异常
     */
    public boolean deleteFile(String path) throws Exception {
        if (path.isBlank()) {
            return false;
        }
        if (!existFile(path)) {
            return false;
        }

        Path srcPath = new Path(path);
        boolean isOk = fs.deleteOnExit(srcPath);
        fs.close();
        return isOk;
    }

    /**
     * 上传HDFS文件
     *
     * @param path 文件路径
     * @param uploadPath 上传路径
     * @throws Exception 异常
     */
    public void uploadFile(String path, String uploadPath) throws Exception {
        if (path.isBlank() || uploadPath.isBlank()) {
            return;
        }

        // 上传路径
        Path clientPath = new Path(path);
        // 目标路径
        Path serverPath = new Path(uploadPath);

        // 调用文件系统的文件复制方法，第一个参数是否删除原文件true为删除，默认为false
        fs.copyFromLocalFile(false, clientPath, serverPath);
        fs.close();
    }

    /**
     * 下载HDFS文件
     *
     * @param path 文件路径
     * @param downloadPath 下载路径
     * @throws Exception 异常
     */
    public void downloadFile(String path, String downloadPath) throws Exception {
        if (path.isBlank() || downloadPath.isBlank()) {
            return;
        }

        // 上传路径
        Path clientPath = new Path(path);
        // 目标路径
        Path serverPath = new Path(downloadPath);

        // 调用文件系统的文件复制方法，第一个参数是否删除原文件true为删除，默认为false
        fs.copyToLocalFile(false, clientPath, serverPath);
        fs.close();
    }

    /**
     * HDFS文件复制
     *
     * @param sourcePath 源路径
     * @param targetPath 目标路径
     * @throws Exception 异常
     */
    public void copyFile(String sourcePath, String targetPath) throws Exception {
        if (sourcePath.isBlank() || targetPath.isBlank()) {
            return;
        }

        // 原始文件路径
        Path oldPath = new Path(sourcePath);
        // 目标路径
        Path newPath = new Path(targetPath);

        FSDataInputStream inputStream = fs.open(oldPath);
        FSDataOutputStream outputStream = fs.create(newPath);
        IOUtils.copyBytes(inputStream, outputStream, bufferSize, true);
    }

    /**
     * 打开HDFS上的文件并返回byte数组
     *
     * @param path 文件路径
     * @return {@code byte[]}
     * @throws Exception 异常
     */
    public byte[] openFileToBytes(String path) throws Exception {
        if (path.isBlank()) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }

        // 目标路径
        Path srcPath = new Path(path);
        try {
            FSDataInputStream inputStream = fs.open(srcPath);
            return IOUtils.readFullyToByteArray(inputStream);
        } finally {
            fs.close();
        }
    }

    /**
     * 打开HDFS上的文件并返回java对象
     *
     * @param path  文件路径
     * @param clazz 目标实体类
     * @return {@code T}
     * @throws Exception 异常
     */
    public <T> T openFileToObject(String path, Class<T> clazz) throws Exception {
        if (path.isBlank()) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }
        String jsonStr = readFile(path);
        return JSON.parseObject(jsonStr, clazz);
    }

    /**
     * 获取某个文件在HDFS的集群位置
     *
     * @param path 文件路径
     * @return {@code BlockLocation[]}
     * @throws Exception 异常
     */
    public BlockLocation[] getFileBlockLocations(String path) throws Exception {
        if (path.isBlank()) {
            return null;
        }
        if (!existFile(path)) {
            return null;
        }

        // 目标路径
        Path srcPath = new Path(path);
        FileStatus fileStatus = fs.getFileStatus(srcPath);
        return fs.getFileBlockLocations(fileStatus, 0, fileStatus.getLen());
    }

}